/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#ifndef __QS_StockDatabase_H__
#define __QS_StockDatabase_H__

#include "StockInfo.h"
#include "DBIndexFile.h"
#include "DBDayFile.h"
#include "StockShareInfo.h"

namespace QSThink
{

	class StockSymbol
	{
	public:
		StockSymbol(QString symbol,QString name,QString shortname,QDate date):
		  _symbol(symbol),_name(name),_shortname(shortname),_datePublic(date)
		{

		}

	public:
		QString symbol(){
			return _symbol;
		}
		QString name(){
			return _name;
		}
		QString shortname(){
			return _shortname;
		}

		QDate datePublic(){
			return _datePublic;
		}
	protected:
		QString _symbol;
		QString _name;
		QString _shortname;
		QDate _datePublic;//on public date

	};
	

	

	/*manipulate stock DB
	*/
	class StockDatabase
	{
	public:
		typedef std::map<int,DBDayFile*> DayDataMap;

		static StockDatabase& getSingleton();

		
		StockDatabase();
		
		void init();

		//every time we calculate it,so pointer is no need.
		//
		double getHighestPriceEx(QString symbol,QDate startDate,QDate endDate);

		double getLowestPriceEx(QString symbol,QDate startDate,QDate endDate);


		StockDayInfo* getStockInfo(int day, int symbol );

		StockShareInfo* getStockShareInfo(QString symbol);
		double getStockCapital(QString symbol,QDate date);
		double getStockCapital(QString symbol,int day);


		//power mean the equivalent stock count of current 1 stock at specified day
		double getStockPower(QString symbol,int day);

		//not the really public date but this day is very important for
		//the stock's data start with this day 
		QDate getStockPublicDate(QString symbol);

		int getStockPublicDay(QString symbol);

		std::string getStockName( int symbol );
		bool hasSymbol(QString symbol);
		QString getStockName(QString symbol);


		bool persistStockInfo(StockInfo stockInfo);
		DBDayFile* getDayFile( int day );

		//get stock location(index,id) in this system by symbol
		int getSymbolIndex( int symbol );
		void loadSymbols(StockSymbolType type);

		
		//start download last day's info
		void startDownload();

		///////////
		void loadSymbolList();

		int getStockSymbolCount()
		{
			return _symbols.size();
		}

		StockSymbol *getStockSymbol(int index)
		{
			return _symbols[index];

		}

		int getSymbolsCount( CultureType cul, ExchangeType ex, SectorType sec );

		QString getSymbol(CultureType cul, ExchangeType ex, SectorType sec ,int index);
		QString getName(CultureType cul, ExchangeType ex, SectorType sec ,int index);

		QString getName(QString symbol);
		QString getSymbol(QString name);


		int getChinaSymbolsCount( ExchangeType ex, SectorType sec );
		QString getChinaName( ExchangeType ex, SectorType sec, int index );
		QString getChinaSymbol( ExchangeType ex, SectorType sec, int index );
		QString getChinaShangHaiName( SectorType sec, int index );
		QString getChinaShenZhenName( SectorType sec, int index );
		QString getChinaAllName( SectorType sec, int index );
		QString getChinaAllSymbol( SectorType sec, int index );
		QString getChinaShenZhenSymbol( SectorType sec, int index );
		QString getChinaShangHaiSymbol( SectorType sec, int index );
		float getStockBalancedDayPrice( QString symbol, QDate date );
		float getStockBalancedDayPrice( QString symbol, int day );

	protected:
		typedef std::map<QString,QString> StringMap;
		typedef std::vector<QString> StringVector;

		std::vector<StockSymbol*> _symbols;

		StringMap _symbolToNameMap;
		StringMap _nameToSymbolMap;

		StringVector _namesShangHai;
		StringVector _namesShenZhen;
		StringVector _namesShenZhenExt;

		StringVector _symbolsShangHai;
		StringVector _symbolsShenZhen;
		StringVector _symbolsShenZhenExt;


		DBIndexFile* _indexFile;
		DBDayIndexFile* _dayIndex;
		DayDataMap _dataFiles;//search by day


	protected:
		typedef std::map<QString,StockShareInfo*> StockShareInfoMap;
		StockShareInfoMap _shareInfos;
	};


}

#endif
